//
//  ViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ViewController.h"
#import "DetailViewController.h"
#import "firstCell.h"
#import "Student.h"
#import "AddViewController.h"
#import "CoreDataManager.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *array;

@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation ViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self search];
    [self.manager saveContext];
    [self.tableView reloadData];
    
    
}

- (void)search {
    self.manager = [CoreDataManager defaults];
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    NSPredicate *predicate = nil;
    request.predicate = predicate;
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error = nil;
    self.array = [NSMutableArray array];
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    self.array = (NSMutableArray *)array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(click:)];
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[firstCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
}


- (void)click:(UIBarButtonItem *)item {
    AddViewController *addVC = [[AddViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    firstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.nameLabel.text = [self.array[indexPath.row] name];
    cell.sexLabel.text = [self.array[indexPath.row] sex];
    cell.numberLabel.text = [self.array[indexPath.row] number].stringValue;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.name = [self.array[indexPath.row] name];
    detailVC.sex = [self.array[indexPath.row] sex];
    detailVC.number = [self.array[indexPath.row] number];
    detailVC.phone = [self.array[indexPath.row] phone];
    detailVC.QQ = [self.array[indexPath.row] qq];
    detailVC.address = [self.array[indexPath.row] addressMap];
    detailVC.i = indexPath.row;
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self search];
        [self.manager.managedObjectContext deleteObject:self.array[indexPath.row]];
        [self.manager saveContext];
        [self search];
        [self.tableView reloadData];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
